import 'dart:io';

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:my_demo_flutter/my_base_lib/base_lib.dart';

class DioTestPage extends StatefulWidget {
  static String routeName = "/DioTestPage";

  const DioTestPage({Key? key}) : super(key: key);

  @override
  _DioTestPageState createState() => _DioTestPageState();
}

class _DioTestPageState extends State<DioTestPage> {
  String responseJsonStr = "";

  @override
  Widget build(BuildContext context) {



    return Scaffold(
      appBar: AppBar(
        title: Text("dio请求接口"),
      ),
      body: Padding(
        padding: EdgeInsets.all(10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            OutlinedButton(onPressed: testGet, child: Text("get请求（天气预报接口）")),
            OutlinedButton(onPressed: testDownload, child: Text("下载视频文件测试")),
            SizedBox(
              height: 80,
            ),
            Text("响应内容"),
            Expanded(
                child: SingleChildScrollView(
              child: Text(responseJsonStr),
            ))
          ],
        ),
      ),
    );
  }

  testGet() async {
    LogUtils.e("get请求,发起请求");
    Response response = await DioUtils.baseGet(
        "http://t.weather.itboy.net/api/weather/city/101010100");
    LogUtils.e("get请求,响应：${response.statusCode} $response");
    setState(() {
      responseJsonStr = response.toString();
      //可以在这里将json字符串转对象
    });
  }

  testDownload() async{
    String savePath =FileUtils.getDefaultPathByFileName("load2345.mp4");
    DioUtils.download(
        "https://media.w3.org/2010/05/sintel/trailer.mp4", savePath,
        onSuccess: () => print("下载成功"),
        onFailed: (msg) => print("下载失败:$msg"),
        onProgress: (progress) => print("下载进度：$progress"),
    );

  }
}
